import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.sql.Date;
import java.sql.Time;

public final class UserMessageEditor {
	/**
	 * This variable is the juice of the break.
	 */
	private static final String breakSign = MessageEditor.breakSign;
	/**
	 * This variable needs to have at least one space on each side of it ex: " breakSign "
	 */
	private static final String dBreak = " " + breakSign + " ";
	public static User getUser(String message){
		try{
			Scanner scan = new Scanner(message);

			if(!scan.next().equals("USER")) return null;
			if(!scan.next().equals(breakSign)) return null;

			String userName, password;
			boolean[] roles = new boolean[User.NUMROLES];
			userName = "";
			password = "";
			String next = scan.next();

			while(!next.equals(breakSign)){
				userName += next + " ";
				next = scan.next();
			}next = scan.next();

			while(!next.equals(breakSign)){
				password += next + " ";
				next = scan.next();
			}

			boolean suspended = Boolean.parseBoolean(scan.next());
			if(!scan.next().equals(breakSign)) return null;

			for(int i = 0; i < User.NUMROLES; i++){
				roles[i] = Boolean.parseBoolean(scan.next());
				if(!scan.next().equals(breakSign)) return null;
			}

			int ssn = Integer.parseInt(scan.next());
			if(!scan.next().equals(breakSign)) return null;

			User user = new User(userName, password, roles, ssn);
			user.setSuspended(suspended);
			String name, address, phone, email, pharmacy, insurance, cellPhone, allergies, gender;
			name = "";
			address = "";
			phone = "";
			email = "";
			pharmacy = "";
			insurance = "";
			cellPhone = "";
			allergies = "";

			next = scan.next();
			while(!next.equals(breakSign)){
				name += next + " ";
				next = scan.next();
			}next = scan.next();
			while(!next.equals(breakSign)){
				address += next + " ";
				next = scan.next();
			}next = scan.next();
			while(!next.equals(breakSign)){
				phone += next + " ";
				next = scan.next();
			}next = scan.next();
			while(!next.equals(breakSign)){
				email += next + " ";
				next = scan.next();
			}next = scan.next();
			while(!next.equals(breakSign)){
				pharmacy += next + " ";
				next = scan.next();
			}next = scan.next();
			while(!next.equals(breakSign)){
				insurance += next + " ";
				next = scan.next();
			}next = scan.next();
			while(!next.equals(breakSign)){
				cellPhone += next + " ";
				next = scan.next();
			}next = scan.next();
			while(!next.equals(breakSign)){
				allergies += next + " ";
				next = scan.next();
			}

			user.setName(name);
			user.setAddress(address);
			user.setPhone(phone);
			user.setEmail(email);
			user.setPharmacy(pharmacy);
			user.setInsurance(insurance);
			user.setCellPhone(cellPhone);
			user.setAllergies(allergies);

			gender = scan.next();
			if(gender.equalsIgnoreCase("male")){
				user.setGender(Gender.MALE);
			}else if(gender.equalsIgnoreCase("female")){
				user.setGender(Gender.FEMALE);
			}else{
				user.setGender(Gender.NULL);
			}
			if(!scan.next().equals(breakSign)) return null;

			user.setBirthDate(Date.valueOf(scan.next()));
			if(!scan.next().equals(breakSign)) return null;

			if(!scan.next().equals("MEDICALHISTORY")) return null;
			if(!scan.next().equals(breakSign)) return null;

			next = scan.next();
			while(next.equals("TREATMENTRECORD")){
				String str = next + " ";
				next = scan.next();
				while(!next.equals("END")){
					str += next + " ";
					next = scan.next();
				}
				str += "END";
				TreatmentRecord rec = TreatmentRecordMessageEditor.getTreatmentRecord(str);
				user.addTreatmentRecord(rec);
				if(!scan.next().equals(breakSign)) return null;
				next = scan.next();
			}

			if(!next.equals("END")) return null;
			return user;
		}catch(Exception e){
			return null;
		}
	}
	public static String getMessage(User user){
		if(user == null){
			return null;
		}else{
			String msg = "USER" + dBreak
			+ user.getUserName() + dBreak
			+ user.getPassword() + dBreak
			+ user.getSuspended() + dBreak;
			boolean[] roles = user.getRoles();
			msg += roles[0] + dBreak
			+ roles[1] + dBreak
			+ roles[2] + dBreak
			+ roles[3] + dBreak
			+ user.getSSN() + dBreak
			+ user.getName() + dBreak
			+ user.getAddress() + dBreak
			+ user.getPhone() + dBreak
			+ user.getEmail() + dBreak
			+ user.getPharmacy() + dBreak
			+ user.getInsurance() + dBreak
			+ user.getCellPhone() + dBreak
			+ user.getAllergies() + dBreak
			+ user.getGender() + dBreak
			+ user.getBirthDate().toString() + dBreak
			+ "MEDICALHISTORY" + dBreak;
			List<TreatmentRecord> list = user.getTreatmentRecords();
			for(int i = list.size() - 1; i > -1; i--){
				msg += TreatmentRecordMessageEditor.getMessage(list.get(i))
				+ dBreak;
			}
			msg += "END";
			return msg;
		}
	}
	public static void main(String[] args){
		boolean[] roles = {true, false, false, false};
		User user = new User("bjones4", "password", roles, 2315, "Bob Jones");
		user.setAddress("167 Something Road, Atlanta GA 30456");
		user.setPhone("(555) 837 - 7256");
		user.setEmail("bjones4@soandso.org");
		user.setPharmacy("Typical Medicines Inc.");
		user.setInsurance("Loan Shark Insurance");
		user.setCellPhone("(555) 111 - 4783");
		user.setAllergies("Dogs, cats, goldfish, and dustmites");
		user.setGender(Gender.MALE);
		user.setBirthDate(new Date(78, 2, 18));
		TreatmentRecord tr = new TreatmentRecord();
		tr.setAttendingNurse("Silly Billy");
		tr.setBpDiastolic(4);
		tr.setBpSystolic(16);
		tr.setDate(new Date(12, 10, 12));
		tr.setDiagnosis("The patient is very, very sick.");
		tr.setFollowUpInstructions("Do stuff after this appointment.");
		tr.setOtherInstructions("Do other stuff too.");
		tr.setHeight(new Height(12, 12));
		tr.setLabWork("This lab work needs to get done.");
		tr.setPrescription("Take these pills.");
		tr.setPulse(17);
		tr.setSymptoms("This stuff is happening");
		tr.setTemperature(5.68);
		tr.setTime(new Time(3, 45, 32));
		tr.setTreatingDoctor("Dr. John Whatever");
		tr.setWeight(8.9);
		TreatmentRecord tr2 = new TreatmentRecord();
		tr2.setAttendingNurse("Sally Jones");
		tr2.setBpDiastolic(4);
		tr2.setBpSystolic(16);
		tr2.setDate(new Date(12, 10, 12));
		tr2.setDiagnosis("The patient is very, very sick.");
		tr2.setFollowUpInstructions("Do stuff after this appointment.");
		tr2.setOtherInstructions("Do other stuff too.");
		tr2.setHeight(new Height(12, 12));
		tr2.setLabWork("This lab work needs to get done.");
		tr2.setPrescription("Take these pills.");
		tr2.setPulse(17);
		tr2.setSymptoms("This stuff is happening");
		tr2.setTemperature(5.68);
		tr2.setTime(new Time(3, 45, 32));
		tr2.setTreatingDoctor("Dr. Jim Somebody");
		tr2.setWeight(8.9);
		user.addTreatmentRecord(tr);
		user.addTreatmentRecord(tr2);
		System.out.println(user);
		String msg = getMessage(user);
		System.out.println("\n\n" + msg + "\n" + msg.length());
		User user2 = getUser(msg);
		System.out.println(user2);
	}
}

